﻿using UnityEngine;

namespace Assets.Scripts.Game.GameObj
{
    public class Unit : WorldObject {

        public Unit()
        {            
            CurObjType = ObjType.Unit;
        }

        private MoveParam _moveParam;
    
        public bool Do_Move(float tick,float speed)
        {

            Position curPos = Pos;
            Position targetPos = _moveParam.GetCurTargetPos();
            float disToTarget = Utils.Distance(curPos, targetPos);
            float disMove = Time.deltaTime * speed;
            Debug.Log(disMove);

            while(true)
            {
                if (disMove > disToTarget)
                {
                    if (!_moveParam.NextTargetPos())
                    {
                        SetPos(targetPos);
                        OnMoveStop();
                 
                        break;
                    }
                    else
                    {
                        disMove -= disToTarget;
                        curPos = targetPos;
                        targetPos = _moveParam.GetCurTargetPos();
                        disToTarget = Utils.Distance(curPos, targetPos);

                        SetDirection((int)Utils.Direct(curPos, targetPos));
                    }
                }
                else
                {
                    Position newPos = new Position();
                    if (disToTarget > 0.0f)
                    {
                        newPos.X = curPos.X + (int)(disMove * (targetPos.X - curPos.X) / disToTarget);
                        newPos.Y = curPos.Y + (int)(disMove * (targetPos.Y - curPos.Y) / disToTarget);
                    }
                    else
                    {
                        newPos.X = targetPos.X;
                        newPos.Y = targetPos.Y;
                    }

                    SetPos(newPos);
                    break;
                }
            }
        
            return true;
        }


        public void OnMoveStart()
        {
      
        }

        public void OnMoveStop()
        {

        }

    }
}
